﻿/************************************
Authors
Tandin Wangchuk (u3057074) & Hesham (u567890)
Assignment 2
15/04/2011
***********************************
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MyPhotoShop
{
    public partial class frmMyPhotoShop : Form
    {
        Image myImage;
        ToolStripMenuItem curZoom;
        ImageProcess myFilter;
        double zoomFactor = 1.0;
        
        public frmMyPhotoShop()
        {
            InitializeComponent();
            myImage = new Image();
            curZoom = mnuViewZoom100; 
        }
        private void frmMyPhotoShop_Load(object sender, EventArgs e)
        {

        }
        private void myPhotoShop_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(myImage.CurBitmap, new Rectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor)));
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Make sure user really wants to exit
            if (MessageBox.Show("Are you sure you want to exit?", "Exit Application",MessageBoxButtons.YesNo, MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return;
            }
            else
            {
                this.Close();
            }
        }
        
        private void mnuFileSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSave = new SaveFileDialog();
            dlgSave.InitialDirectory = "c:\\";
            dlgSave.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
            dlgSave.FilterIndex = 2;
            dlgSave.RestoreDirectory = true;
            if (DialogResult.OK == dlgSave.ShowDialog())
            {
                try
                {
                    myImage.BitmapPath = dlgSave.FileName;
                    if (System.IO.File.Exists(dlgSave.FileName))
                        System.IO.File.Delete(dlgSave.FileName);
                    myImage.CurBitmap.Save(dlgSave.FileName);
                     MessageBox.Show("Picture Saved Successfully."); 
                }
                catch (Exception saveEx)
                {
                    MessageBox.Show(saveEx.Message, "Error Saving Image", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void pbImage_Click(object sender, EventArgs e)
        {
            //pbImage.SizeMode = PictureBoxSizeMode.AutoSize;
        }

        private void mnuCrop_Click(object sender, EventArgs e)
        {
            CropInput cropIn = new CropInput();
            cropIn.XPosition = 0;
            cropIn.YPosition = 0;
            cropIn.cWidth = myImage.CurBitmap.Width;
            cropIn.cHeight = myImage.CurBitmap.Height;
            if (cropIn.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                myImage.RestorePreviousBitmap();
                myImage.DrawCropArea(cropIn.XPosition, cropIn.YPosition, cropIn.cWidth, cropIn.cHeight);
                this.Invalidate();
                if (MessageBox.Show("Do u want to crop this area?", "ImageProcessing", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    myImage.Crop(cropIn.XPosition, cropIn.YPosition, cropIn.cWidth, cropIn.cHeight);
                }
                else
                {
                    myImage.RemoveCropAreaDraw();
                }
                this.Invalidate();
                this.Cursor = Cursors.Default;
            }
        }

        private void mnuColorBrightness_Click(object sender, EventArgs e)
        {
            BrightnessInput brightFrm = new BrightnessInput();
            brightFrm.Value = 0;
            myFilter = new Filters("Brightness");
            if (brightFrm.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                myImage.RestorePreviousBitmap();
                myFilter.SetBrightness(myImage, brightFrm.Value);
                this.Invalidate();
                this.Cursor = Cursors.Default;
            }
        }
        
        private void mnuColorContrast_Click(object sender, EventArgs e)
        {
            ContrastInput contrastFrm = new ContrastInput();
            contrastFrm.Value = 0;
            if (contrastFrm.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                myImage.RestorePreviousBitmap();
                myFilter.SetContrast(myImage,contrastFrm.Value);
                this.Invalidate();
                this.Cursor = Cursors.Default;
            }
        }
        private void mnuColorGrayScale_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            myImage.RestorePreviousBitmap();
            myFilter = new Filters("GrayScale");
            myFilter.SetGrayscale(myImage);
            this.Invalidate();
            this.Cursor = Cursors.Default;
        }
        
        private void mnuColorInvert_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            myImage.RestorePreviousBitmap();
            myFilter = new Filters("Invert");
            myFilter.SetInvert(myImage);
            this.Invalidate();
            this.Cursor = Cursors.Default;
        }

        private void mnuEditUndo_Click(object sender, EventArgs e)
        {
            myImage.ResetBitmap();
            this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
            this.Invalidate();
        }
        
        private void mnuFileOpen_Click(object sender, EventArgs e)
        {

            try
            {
                OpenFileDialog show = new OpenFileDialog();
                show.InitialDirectory = "c:\\";
                show.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
                show.FilterIndex = 2;
                show.RestoreDirectory = true;
                if (show.ShowDialog() == DialogResult.OK)
                {
                    myImage.CurBitmap = (Bitmap)Bitmap.FromFile(show.FileName);
                    myImage.BitmapPath = show.FileName;
                    this.AutoScroll = true;
                    this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
                    this.Invalidate();
                    mnuViewImageInfo.Enabled = true;
                    ImageInfo imgInfo = new ImageInfo(myImage.BitmapPath, myImage.CurBitmap);
                    imgInfo.Show();
                }
            }

            catch (Exception loadEx)
            {
                MessageBox.Show(loadEx.Message, "Error loading Image", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void mnuImageResize_Click(object sender, EventArgs e)
        {
            ResizeForm rsFrm = new ResizeForm();
            rsFrm.NewWidth = myImage.CurBitmap.Width;
            rsFrm.NewHeight = myImage.CurBitmap.Height;
            if (rsFrm.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                myImage.RestorePreviousBitmap();
                myImage.Resized(rsFrm.NewWidth, rsFrm.NewHeight);
                this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
                this.Invalidate();
                this.Cursor = Cursors.Default;
            }
        }

        
        private void mnuEditClearImage_Click(object sender, EventArgs e)
        {
            myImage.RestorePreviousBitmap();
            myImage.ClearImage();
            this.Invalidate();
        }
        private void ZoomFactor(double zoom)
        {
            this.AutoScrollMinSize = new Size((int)(myImage.CurBitmap.Width * zoom), (int)(myImage.CurBitmap.Height * zoom));
            this.curZoom.Checked = false;
        }

        private void mnuViewZoom50_Click(object sender, EventArgs e)
        {
            zoomFactor = .50;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom50.Checked = true;
            this.curZoom = mnuViewZoom50;
            this.Invalidate();
        }

        private void mnuViewZoom100_Click(object sender, EventArgs e)
        {
            zoomFactor = 1.0;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom100.Checked = true;
            this.curZoom = mnuViewZoom100;
            this.Invalidate();
        }

        private void mnuViewZoom150_Click(object sender, EventArgs e)
        {
            zoomFactor = 1.5;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom150.Checked = true;
            this.curZoom = mnuViewZoom150;
            this.Invalidate();
        }

        private void mnuViewZoom200_Click(object sender, EventArgs e)
        {
            zoomFactor = 2.0;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom200.Checked = true;
            this.curZoom = mnuViewZoom200;
            this.Invalidate();
        }

        private void mnuViewZoom250_Click(object sender, EventArgs e)
        {
            zoomFactor = 2.5;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom250.Checked = true;
            this.curZoom = mnuViewZoom250;
            this.Invalidate();
        }

        private void mnuViewZoom300_Click(object sender, EventArgs e)
        {
            zoomFactor = 3.0;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom300.Checked = true;
            this.curZoom = mnuViewZoom300;
            this.Invalidate();
        }

        private void mnuViewZoom350_Click(object sender, EventArgs e)
        {
            zoomFactor = 3.5;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom350.Checked = true;
            this.curZoom = mnuViewZoom350;
            this.Invalidate();
        }

        private void mnuViewZoom400_Click(object sender, EventArgs e)
        {
            zoomFactor = 4.0;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom400.Checked = true;
            this.curZoom = mnuViewZoom400;
            this.Invalidate();
        }

        private void mnuViewZoom450_Click(object sender, EventArgs e)
        {
            zoomFactor = 4.5;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom450.Checked = true;
            this.curZoom = mnuViewZoom450;
            this.Invalidate();
        }

        private void mnuViewZoom500_Click(object sender, EventArgs e)
        {
            zoomFactor = 5.0;
            ZoomFactor(zoomFactor);
            this.mnuViewZoom500.Checked = true;
            this.curZoom = mnuViewZoom500;
            this.Invalidate();
        }

        private void mnuColorFilerRed_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            myImage.RestorePreviousBitmap();
            myFilter.SetColorFilter(myImage,"Red");
            this.Invalidate();
            this.Cursor = Cursors.Default;
        }

        private void mnuColorFilterGreen_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            myImage.RestorePreviousBitmap();
            myFilter.SetColorFilter(myImage, "Green");
            this.Invalidate();
            this.Cursor = Cursors.Default;
        }

        private void mnuColorFilterBlue_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            myImage.RestorePreviousBitmap();
            myFilter.SetColorFilter(myImage, "Blue");
            this.Invalidate();
            this.Cursor = Cursors.Default;
        }

        private void mnuImageRotate90_Click(object sender, EventArgs e)
        {
            myImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
            this.AutoScroll = true;
            this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
            this.Invalidate();
        }

        private void mnuImageRotate180_Click(object sender, EventArgs e)
        {
            myImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
            this.AutoScroll = true;
            this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
            this.Invalidate();
        }

        private void mnuImageRotate270_Click(object sender, EventArgs e)
        {
            myImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
            this.AutoScroll = true;
            this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
            this.Invalidate();
        }

        private void mnuImageFlipHorizontal_Click(object sender, EventArgs e)
        {
            myImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
            this.AutoScroll = true;
            this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
            this.Invalidate();
        }

        private void mnuImageFlipVertical_Click(object sender, EventArgs e)
        {
            myImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
            this.AutoScroll = true;
            this.AutoScrollMinSize = new Size(Convert.ToInt32(myImage.CurBitmap.Width * zoomFactor), Convert.ToInt32(myImage.CurBitmap.Height * zoomFactor));
            this.Invalidate();
        }

        private void mnuColorGamma_Click(object sender, EventArgs e)
        {
            GammaInput gForm = new GammaInput();
            gForm.Red = gForm.Green = gForm.Blue = 0;
            myFilter = new Filters("Gamma");
            if (gForm.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                myImage.RestorePreviousBitmap();
                myFilter.SetGamma(myImage,gForm.Red, gForm.Green, gForm.Blue);
                this.Invalidate();
                this.Cursor = Cursors.Default;
            }
        }
             
    }
}
